
var regexp = new RegExp("(<.+?>).+(<\/.+?>)");

/**
 * date: 2018年2月28日
 * 测试段落元素的拆分(利用正则表达式)
 * 
 */
$(function(){
	$('#btnTest').on('click', function(){
		var divTest = $('#test');
		divTest.empty();
		divTest.append('<p><span id="span002">现病史:<span>患者于<b>半年余前</b>无明显诱因下出现右上腹部疼痛，</span>呈持续性闷痛，逐渐<b>加重，<span class="emrbreak"></span>开始</b>无伴有皮肤黄染，未呈桔黄色，无尿色变黄，无呈浓茶样改变</span></p>');
		var targetEle = divTest.children().eq(0);
		var emrbreak = targetEle.find('span.emrbreak').eq(0);
		var allNecessaryParents = [];
		/**
		 * API: http://api.jquery.com/parents/
		 * 
		 * `parents()`会将'body','html'都拿出来
		 */
		$.each(emrbreak.parents(), function(i,n){
			// 在这个例子中只有一个`div`,所以可以这样写.实际开发过程中要视情况而随机应变
			if(!checkInArr($(n).get(0).tagName, ['body','html','div'])){
				// console.log(i, $(n).get(0).tagName );
				allNecessaryParents.push( n );
			}
		});
		var len = allNecessaryParents.length;
		var startTagGather = '', endTagGather = '';
		var endTagArr = [];
		for(var i=len-1;i>=0;i--){
			//console.log( allNecessaryParents[i] );
        	if (regexp.test($(allNecessaryParents[i])[0].outerHTML)) {
        		startTagGather += RegExp.$1;
        		endTagArr.push(RegExp.$2);
            }
		}
		var len2 = endTagArr.length;
		for(var j=len2-1;j>=0;j--){
			endTagGather += endTagArr[j];
		}
		console.log('startTagGather: ', startTagGather);
		console.log('endTagGather: ', endTagGather);
		
		console.log('startTagGather: ', symbolTranslate(startTagGather));
		console.log('endTagGather: ', symbolTranslate(endTagGather));
		
		console.log('===');
		
		var firstPartReg = new RegExp('(.*)(?=<span class="emrbreak"></span>)');
		var secondPartReg = new RegExp('(?<=<span class="emrbreak"></span>)(.*)');
		var firstPart = '', secondPart = '';
		if( firstPartReg.test(targetEle.get(0).outerHTML) ){
			firstPart = RegExp.$1;
			// console.log( RegExp.$1 );
		}
		if( secondPartReg.test(targetEle.get(0).outerHTML) ){
			secondPart = RegExp.$1;
			// console.log( RegExp.$1 );
		}
		firstPart = firstPart + endTagGather;
		secondPart = startTagGather + secondPart;
		console.log('== firstPart ==');
		console.log( firstPart );
		console.log('== secondPart ==');
		console.log( secondPart );
	});
});

/**
 * 替换特殊字符
 * 
 * @param htmlStr
 * @returns
 */
function symbolTranslate(htmlStr){
	/**
	 * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace
	 */
	return htmlStr.replace(/[<>]/g, function(match){
		return {'<':'&lt;','>':'&gt;'}[match];
	});
}

/**
 * 检查一个元素是否在一个数组中
 */
function checkInArr(val, arr, ignoreCase){
	var flag = false;
	var val = val || '';
	var arr = arr || [];
	/**
	 * var ignoreCase = ignoreCase || true;
	 * 1) JS陷阱: 上面的代码是不可取的, 如果传入`false`计算的值也是true
	 * var ignoreCase = (ignoreCase == undefined ? true : false);
	 * 2) 上面的代码也是不可取的, 如果传入`true`计算的值也是false
	 * var ignoreCase = (ignoreCase == undefined ? true : ignoreCase);
	 * 3) 上面的写法才是正确的
	 */
	var ignoreCase = (ignoreCase === undefined ? true : ignoreCase);
	var len = arr.length;
	for(var i=0;i<len;i++){
		if(ignoreCase){
			if( val.toString().toUpperCase() === arr[i].toString().toUpperCase() ){
				flag = true;
				break;
			}
		}else{
			if( val.toString() === arr[i].toString() ){
				flag = true;
				break;
			}
		}
	}
	return flag;
}


